package com.atguigu.gulimallcart.controller;

import com.atguigu.gulimallcart.service.CartService;
import com.atguigu.gulimallcart.vo.Cart;
import com.atguigu.gulimallcart.vo.CartItem;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {


    @Autowired
    CartService cartService;




    /**
     * 获取购物车数据 - 去购物车页面
     * 游览器有个cookie user-key标识用户身份，一个月后过期
     * 如果第一次使用购物车，会给用户一个临时身份
     * 游览器保存之后每次访问都会带上这个key
     *
     *  登录 session有
     *  没登录 按照cookie 中的user-key
     *  第一次，分配一个user-key
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {

        Cart cart = cartService.getCart();
        model.addAttribute("cart", cart);

        return "cartList";
    }

    /**
     * 添加购物车 - 添加成功转发到成功页面（在成功页面查询购物项的信息，防止刷新时重复提交 商品数量增加）
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {

            cartService.addToCart(skuId, num);
            // .addFlashAttribute() 将数据放在session中，但只能取一次
            // .addAttribute() 将数据拼接到url后面
            redirectAttributes.addAttribute("skuId", skuId);



        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    /**
     * 添加购物车成功 - 跳转到成功页面
     * @param skuId
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId, Model model) {

        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("cartItem", cartItem);
        return "success";
    }


    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check")Integer check){

        cartService.checkItem(skuId,check);
        return "redirect:http://cart.gulimall.com/cart.html";

    }


    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId")Long skuId, @RequestParam("count")Integer count) {
        cartService.countItem(skuId,count);
        return "redirect:http://cart.gulimall.com/cart.html";

    }

    @GetMapping("/delItem")
    public String delItem(@RequestParam("skuId")Long skuId) {
        cartService.delItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";

    }

    /**
     * 获取当前登录用户选中的购物项
     * @return
     */
    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItem> currentUserCartItems() {
        System.out.println("到达 /currentUserCartItems ");
        List<CartItem> cartItems = cartService.currentUserCartItems();
        return cartItems;
    }

    @GetMapping("/haha")
    @ResponseBody
    public String haha() {
        return "haha";
    }
}
